package com.example.medicalretrieval.adapter;

import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.medicalretrieval.MyApplication;
import com.example.medicalretrieval.R;

import com.example.medicalretrieval.button.CustomRadioButton;
import com.example.medicalretrieval.dao.DownloadHistoryDao;
import com.example.medicalretrieval.pojo.DownloadHistory;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class DownloadHistoryAdapter extends BaseAdapter {
    private List<DownloadHistory> mDownloadHistoryList = null;
    private Context mContext = null;
    private CustomRadioButton button;

    public DownloadHistoryAdapter(Context context,List<DownloadHistory> downloadHistoryList){
        mDownloadHistoryList = downloadHistoryList;
        mContext = context;
    }
    @Override
    public int getCount() {
        return mDownloadHistoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDownloadHistoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_download,null);
            holder = new ViewHolder();
            holder.title = convertView.findViewById(R.id.ItemDownloadTitle);
            holder.date = convertView.findViewById(R.id.ItemDownloadDate);
            holder.date = convertView.findViewById(R.id.ItemDownloadAuthors);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        DownloadHistory downloadHistory = mDownloadHistoryList.get(position);

        holder.title.setText(downloadHistory.getTitle());
        Date date = downloadHistory.getDate();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        holder.date.setText(format.format(date));
        holder.authors.setText(downloadHistory.getAuthors());

        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                dialog(downloadHistory,position);
                return true;
            }
        });

        return convertView;
    }
    private void dialog(DownloadHistory downloadHistory, int position){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("刪除下载记录");
        builder.setMessage("你确定要删除该条记录吗？");
        builder.setCancelable(true);
        builder.setPositiveButton("确认", (dialog, which) -> {
            boolean checked = button.isChecked();
            if (checked){
                String filePath = "";
                deleteFile(AUDIO_FILE_PATH+downloadHistory.getTitle());
            }
            DownloadHistoryDao downloadHistoryDao = MyApplication.getInstance().getDownloadHistoryDataBase().downloadHistoryDao();
            downloadHistoryDao.delete(downloadHistory);
            mDownloadHistoryList.remove(position);
            notifyDataSetChanged();
        });
        builder.setNegativeButton("取消", (dialog, which) -> {

        });

        View view = View.inflate(mContext,R.layout.dialog,null);
        button = view.findViewById(R.id.dialog_button);
        builder.setView(view);
        builder.create().show();
    }
    private static final String AUDIO_FILE_PATH =
            Environment.getExternalStorageDirectory().getPath()+"/download/";
    private void deleteFile(String filePath){
        File file = new File(filePath);
        if (file.isFile()){
            boolean delete = file.delete();
            if (delete){
                Toast.makeText(mContext, "源文件删除成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(mContext, "源文件删除失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
    public final class ViewHolder{
        public TextView title;
        public TextView date;
        public TextView authors;
    }
}
